#!/bin/bash
## test home
testdir=$(cd $(dirname $0)/; pwd)
## word home
workdir=$(dirname $testdir)
cd $workdir

module=graph
app=falcon-$module
pidfile=var/app.pid
logfile=var/app.log
control=./control
httpprex="127.0.0.1:6071"
rpcprex="127.0.0.1:6070"

## statistics
function counter(){
    curl -s "$httpprex/counter/all" | python -m json.tool
}

## config
function config(){
    action=$1
    case $action in
        "reload")
            curl -s "$httpprex/config/reload" | python -m json.tool
            ;;
        *)
            curl -s "$httpprex/config" | python -m json.tool
            ;;
    esac
}


function index(){
    e="test.graph.endpoint.niean.1"
    m="test.graph.metric.1"
    t="tag0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    dt="GAUGE"
    step="60"
    curl -s "$httpprex/index/cache/$e/$m/$step/$dt/$t" | python -m json.tool
}

function index_v2(){
    e="test.graph.endpoint.niean/1"
    m="test.graph.metric/1"
    t="tag/0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    dt="GAUGE"
    step="60"
    curl -s "$httpprex/v2/index/cache?e=$e&m=$m&t=$t&type=$dt&step=$step" | python -m json.tool
}

function index_update_all(){
    curl -s "$httpprex/index/updateAll" | python -m json.tool
}

function index_update_one(){
    e="test.graph.endpoint.niean.1"
    m="test.graph.metric.1"
    tags="tag0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    dt="GAUGE"
    step="60"
    curl -s "$httpprex/index/updateOne?EndPoint=$e&Metric=$m&Tags=$tags&DsType=$dt&Step=$step" | python -m json.tool
}

## history
function fetch_history(){
    e="test.graph.endpoint.niean.1"
    m="test.graph.metric.1"
    tags="tag0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    curl -s "$httpprex/history/$e/$m/$tags" | python -m json.tool
}
function fetch_history_v2(){
    e="test.graph.endpoint.niean/1"
    m="test.graph.metric/1"
    tags="tag/0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    curl -s "$httpprex/v2/history?e=$e&m=$m&t=$tags" | python -m json.tool
}

## last
function fetch_last(){
    e="test.graph.endpoint.niean.1"
    m="test.graph.metric.1"
    tags="tag0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    curl -s "$httpprex/last/$e/$m/$tags" | python -m json.tool
}

## send
function api_send(){
    e="test.graph.endpoint.niean.1"
    m="test.graph.metric.1"
    ts=`date +%s`
    dsType="GAUGE"
    step=60
    value=`expr $ts % 60`
    tags="tag0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    curl -s "$httpprex/api/recv/$e/$m/$ts/$step/$dsType/$value/$tags" | python -m json.tool
}

function api_send_v2(){
    e="test.graph.endpoint.niean/1"
    m="test.graph.metric/1"
    ts=`date +%s`
    dsType="GAUGE"
    step=60
    value=`expr $ts % 60`
    tags="tag/0=tag0-dummy-1,tag1=tag1-dummy-1,tag2=tag2-dummy-1"
    curl -s "$httpprex/v2/api/recv?e=$e&m=$m&ts=$ts&step=$step&type=$dsType&v=$value&t=$tags" | python -m json.tool
}


## tail
function tail_log(){
    $control tail
}

## build
function build(){
    $control build
    [ $? -eq 0 ] && echo -e "build ok" || { echo -e "build error"; exit 1; }
}
function start(){
    $control start
}
function stop(){
    $control stop
}


action=$1
case $action in
    "build")
        build
        ;;
    "start")
        start
        ;;
    "stop")
        stop
        ;;
    "restart")
        stop && build && start
        ;;
    "config")
        config $2
        ;;
    "index")
        index
        ;;
    "index2")
        index_v2
        ;;
    "index_update_all")
        index_update_all
        ;;
    "index_update_one")
        index_update_one
        ;;
    "filter")
        filter_recv
        ;;
    "history")
        fetch_history
        ;;
    "history2")
        fetch_history_v2
        ;;
    "last")
        fetch_last
        ;;
    "send")
        api_send
        ;;
    "send2")
        api_send_v2
        ;;
    "tail")
        tail_log
        ;;
    *)
        counter
        ;;
esac

